package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Dept;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
    DeptService deptService = BeanFactory.getBean("deptService",DeptService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //2 调用service层方法，查询所有数据
            List<Dept> list = deptService.findAll();
            //3 响应结果
            return new Result(true, MessageConstant.DELETE_DEPT_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_DEPT_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //调用service层edit方法,执行删除操作
            deptService.delete(ids);
            //响应客户端
            return new Result(true,MessageConstant.DELETE_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.DELETE_DEPT_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
            //调用service层edit方法,执行保存操作
            deptService.edit(dept);
            //响应客户端
            return new Result(true,MessageConstant.EDIT_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_DEPT_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1.获取请求参数
            String deptId = request.getParameter("id");
            //调用service层方法,获取查询结果
            Dept dept = deptService.findById(deptId);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_DEPT_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数并封装
        try {
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
            //调用service层save方法,执行保存操作
            deptService.save(dept);
            //响应客户端
            return new Result(true,MessageConstant.SAVE_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.SAVE_DEPT_FAIL);
        }
    }

    //处理分页查询的请求
    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数
            String strCurrentPage = request.getParameter("currentPage");
            String strPageSize = request.getParameter("pageSize");
            //1.1判断非空
            int currentPage = 1,pageSize = 5;
            if(!StringUtils.isBlank(strCurrentPage)){
                currentPage = Integer.parseInt(strCurrentPage);
            }
            if(!StringUtils.isBlank(strPageSize)){
                pageSize = Integer.parseInt(strPageSize);
            }
            //调用service层方法,获取查询结果
            PageInfo<Dept> pageInfo = deptService.findByPage(currentPage,pageSize);
            //对客户端做出响应
            return new Result(true, MessageConstant.QUERY_DEPT_SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

}
